package com.yumeng.framework.core.config;

import com.yumeng.config.BaseConfiguration;
import com.yumeng.framework.core.mvc.GlobalErrorController;
import com.yumeng.framework.core.mvc.filter.CommFilter;
import com.yumeng.framework.core.mvc.interceptor.ThreadLocalInterceptor;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.stream.Collectors;

/**
 * WebMvc默认配置基类
 *
 * @author wxd
 * @date 2024/5/30 下午10:12
 */
public abstract class WebMvcConfiguration extends BaseConfiguration implements WebMvcConfigurer {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Bean
    public BasicErrorController basicErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties,
                                                     ObjectProvider<ErrorViewResolver> errorViewResolvers) {
        return new GlobalErrorController(errorAttributes, serverProperties.getError(),
                errorViewResolvers.orderedStream().collect(Collectors.toList()));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ThreadLocalInterceptor()).addPathPatterns("/**/**.do");
    }

    /*    @Bean
    public FilterRegistrationBean<CorsFilter> registerCorsFilter(){
        FilterRegistrationBean<CorsFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CorsFilter());
        registration.addUrlPatterns("/*");
        registration.setName("CorsFilter");
        registration.setOrder(0);
        return registration;
    }*/

    /**
     * 可能是启用线程池 理解
     * @param configurer
     */
/*    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }*/

    /**
     * 通用Filter
     * order越小，优先执行
     * @return
     */
    @Bean
    public FilterRegistrationBean<CommFilter> registerCommFilter(){
        FilterRegistrationBean<CommFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CommFilter());
        registration.addUrlPatterns("/*");
        registration.setName("YumengCommFilter");
        registration.setOrder(0);
        return registration;
    }

}
